package com.zretc.auth.controller;

import com.alibaba.fastjson.JSONObject;
import com.zretc.api.pojo.SysUser;
import com.zretc.auth.service.RegistryService;
import com.zretc.common.entity.R;
import com.zretc.common.entity.RespResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/auth")
public class RegistryController {
    @Autowired
    private RegistryService registryService;
    @PostMapping("/registry")
    public RespResult registry(@RequestBody Map<String,Object> map)
    {
        JSONObject temp=new JSONObject((Map<String, Object>) map.get("user"));
        SysUser user=temp.toJavaObject(SysUser.class);
        user.setPassword(new BCryptPasswordEncoder().encode(user.getPassword()));
        String verifyCode=map.get("verifyCode").toString();
//        if(!registryService.checkVerifyCode(verifyCode,user.getPhone()))
//            return R.fail(410,"手机号验证码错误，请核对后再注册");
        //todo:开发阶段先不用这个验证码，正式完成解除注释
        return registryService.registry(user);
    }
    @GetMapping("/sendVerifyCode")
    public RespResult sendVerifyCode(String phone)
    {
        RespResult result=R.fail(412,"验证码服务器异常");
        try {
            result=registryService.sendVerifyCodeMessage(phone);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return result;
    }
}
